-- Version 2.0 for WoW 3.2
-- ADDED: Prints MailGet version on PLAYER_LOGIN
-- ADDED: Slash commands; version, about
-- ADDED: Prints the total amount of money reveived from mails

-- Known issues:
-- When receiving mails with money enclosed, you have to press 'Take All' twice
-- MailGet sometimes leavs items in mails contaning multiple items

-- Register global events

local g_frame = CreateFrame( "Frame", "G_FRAME" );
g_frame:RegisterEvent( "PLAYER_LOGIN" );
g_frame:SetScript( "OnEvent", GlobalEventHandler );

-- Set up slash command

SLASH_MG1 = '/mg';

-- Initialize variables

g_version = 2.0;
g_wow_version = 3.2;
g_interface = 30200;

MGDB = {}
if (MGDB['take_ah'] == nil) then
	MGDB['take_ah'] = false;
end
if (MGDB['pay_cod'] == nil) then
	MGDB['pay_cod'] = false;
end

--make new button

mailButton = CreateFrame("Button", "OpenMailTakeButton", OpenMail, "UIPanelButtonTemplate");
mailButton:Hide();
mailButton:SetWidth(OpenMailReplyButton:GetWidth()-5);
mailButton:SetHeight(OpenMailReplyButton:GetHeight());
mailButton:SetFrameStrata('DIALOG');
mailButton:SetPoint("right","OpenMailReplyButton","left");
mailButton:SetText('Take All');
mailButton:SetScript('OnClick', MG_GetMailItems);
mailButton:SetScript('OnEvent', MG_OnEvent);
mailButton:RegisterEvent('PLAYER_MONEY');
mailButton:RegisterEvent('CHAT_MSG_LOOT');

--some default variables

MG_MailIndex = 1;
MG_MailItemIndex = 0;
MG_MailQueue = {}
MG_MailIndexes = {}
MG_TakeIndex = 1;
MG_TakeMoney = false;

--set some scripts

MG_NextScript = InboxNextPageButton:GetScript('OnClick');
MG_PrevScript = InboxPrevPageButton:GetScript('OnClick');

InboxNextPageButton:SetScript('OnClick', MG_Next);
InboxPrevPageButton:SetScript('OnClick', MG_Prev);

for i=1,7 do 
	mailboxbutton = getglobal('MailItem' .. i .. 'Button');
	mailboxbutton.oldscript = mailboxbutton:GetScript('OnClick');
	mailboxbutton:SetScript('OnClick', MG_OpenMail);
end


if (OpenMailFrame:GetScript('OnShow')) then
	MG_OldShow = OpenMailFrame:GetScript('OnShow');
end

if (OpenMailFrame:GetScript('OnHide')) then
	MG_OldHide = OpenMailFrame:GetScript('OnHide');
end

OpenMailFrame:SetScript('OnShow', MG_Show);
OpenMailFrame:SetScript('OnHide', MG_Hide);

------------------------------------------------------------------------------------
-- master take all button 
------------------------------------------------------------------------------------

bigMailButton = CreateFrame("Button", "MailFrameTakeButton", InboxFrame, "UIPanelButtonTemplate");
bigMailButton:SetWidth(OpenMailReplyButton:GetWidth());
bigMailButton:SetHeight(OpenMailReplyButton:GetHeight());
bigMailButton:SetFrameStrata('DIALOG');
bigMailButton:SetPoint("top","MailFrame","top", -75, -43);
bigMailButton:SetText('Take All');
bigMailButton:SetScript('OnClick', MG_GetAllMailItems);

------------------------------------------------------------------------------------
-- loot history
------------------------------------------------------------------------------------

--lootHistoryWindow = CreateFrame("Frame", "MailGetHistory", UIParent);
--lootHistoryWindow:SetWidth

--OptionsCheckButtonTemplate
checkButton = CreateFrame("CheckButton", "OpenMailTakeAH", bigMailButton, "OptionsCheckButtonTemplate");
checkButton:SetPoint("left", bigMailButton, "right", 10, 10);
checkButton:SetFrameStrata('DIALOG');
OpenMailTakeAHText:SetText('Take AH Winnings');
OpenMailTakeAH:SetScript('OnClick', MG_ToggleAH);
OpenMailTakeAH:SetScript('OnShow', MG_ShowOMTA);
OpenMailTakeAH:SetScale(.75);

--OptionsCheckButtonTemplate
checkButton2 = CreateFrame("CheckButton", "OpenMailPayCOD", bigMailButton, "OptionsCheckButtonTemplate");
checkButton2:SetPoint("left", bigMailButton, "right", 10, -11);
checkButton2:SetFrameStrata('DIALOG');
OpenMailPayCODText:SetText('Pay CODs automatically');
OpenMailPayCOD:SetScript('OnClick', MG_ToggleCOD);
OpenMailPayCOD:SetScript('OnShow', MG_ShowCOD);
OpenMailPayCOD:SetScale(.75);

